/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: RxDealResponseBean
 * @Package com.rx.core.bean
 * @Description: 统一返回处理
 * @author: 陈锦韬
 * @date: 2020\4\19 0019
 * @version V1.0
 * @Copyright: 2020 陈锦韬  All rights reserved.
 */
package com.rx.core.interceptor;

import com.rx.core.base.RxBaseException;
import com.rx.core.base.RxResponse;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.util.EmptyChecker;
import feign.Request;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.LinkedHashMap;

/**
 * @author: Administrator
 * @Description: 统一返回处理
 * @date: 2020\4\19 0019
 */
@ControllerAdvice
public class RxDealResponseBean implements ResponseBodyAdvice {
    public static final String JSON = "json";
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    private RxBaseException getRxException(Throwable throwable){
        Throwable cause = throwable.getCause();
        if (cause instanceof  RxBaseException){
            return (RxBaseException)cause;
        }
        if(EmptyChecker.isEmpty(cause)){
            return null;
        }
        if (throwable.getClass().equals(cause.getClass())){
            return null;
        }
        if (Throwable.class.equals(throwable.getClass())){
            return null;
        }

        return getRxException(cause);
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (body instanceof RxResponse){
            return body;
        }
        if (!Request.HttpMethod.POST.name().equals(request.getMethod().name())){
            return body;
        }
        if (!selectedContentType.toString().contains(JSON)){
            return body;
        }
//        if (body instanceof InvalidGrantException){
//            RxResponse rxResponse = RxResponse.fail(RESULT_CODE.MESSAGE,"校验错误");
//            rxResponse.setResultData(body);
//            return rxResponse;
//        }
        if (body instanceof RxBaseException){
            return body;
        }
        if (body instanceof Throwable){

            RxBaseException rxBaseException = getRxException((Throwable)body);
            if (EmptyChecker.notEmpty(rxBaseException)){
                return new RxResponse(rxBaseException);
            }
            RxResponse rxResponse = RxResponse.fail(RESULT_CODE.FAIL);
            rxResponse.setResultData(body);
            return rxResponse;
        }
        if (body instanceof LinkedHashMap){
            LinkedHashMap map = (LinkedHashMap)body;
            String error = (String) map.get("error");
            String message = (String) map.get("message");
            if(EmptyChecker.notEmpty(error)&& EmptyChecker.notEmpty(message)){
                return RxResponse.fail(RESULT_CODE.FAIL,error + ":" +message);
            }
        }
        RxResponse rxResponse = new RxResponse();
        rxResponse.setResultData(body);
        return rxResponse;
    }
}
